<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页</title>
<link href="../css/table.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery-1.8.3.min.js"></script>
<script>
var rows={'custName':'姓名','custTellphone':'电话','custWeburl':'主页','industry':'行业','live':'等级','nature':'性质','ctype':'类型','custsource':'来源'};
var data =[];
var pageSize = 10;
var pageCount = 0;
var pageIndex =1;
var rowCount=0;

		$(function(){
			loadPage();
			$('#pageSize_num').change(function(){
				pageIndex=1;
				pageSize=$('#pageSize_num').val();
				if(pageSize<1 || isNaN(pageSize)){
					pageSize=1;
					$('#pageSize_num').val('1');
				}
				loadPage();	
			});
			//设置遮罩
			var container = document.getElementById("showzhezhao");
        	container.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=50,finishOpacity=50)";
        	container.style.opacity = "0.7";	
		});
		
		function reg(){
			$('#nextPage').click(function(){
				pageIndex=pageIndex+1;	
				loadPage();
			});
			$('#upPage').click(function(){
				pageIndex=pageIndex-1;	
				loadPage();
			});	
			$('.page_').click(function(){
				if(!isNaN(this.id)){
					pageIndex=this.id;
					loadPage();
				}
			});
			$("#checkAll").change(function(){
				var ck=document.getElementById("checkAll").checked;
		    	var ckList=document.getElementsByName("check");
		    	for ( var i = 0; i < ckList.length; i++) {
		    		ckList[i].checked=ck;
				}
			});
			$("input[name='check']").click(function(){
				var ck=true;
		    	var ckList=document.getElementsByName("check");
		    	for ( var i = 0; i < ckList.length; i++) {
		    		ck=ck&&ckList[i].checked;
				}
		    	document.getElementById("checkAll").checked=ck;
			});
		}
		
		function loadPage(){
			var name=$("input[name='name']").val();
	    	var tellphone=$("input[name='tellphone']").val();
	    	var industry=$("#industry").val();
	    	var live=$("#live").val();
	    	var nature=$("#nature").val();
	    	var ctype=$("#ctype").val();
	    	var source=$("#source").val();
			$.post('searchCustomer.action',{'currpage':pageIndex,'pagesize':pageSize,'name':name,'tellphone':tellphone,'industry':industry,'live':live,'nature':nature,'ctype':ctype,'source':source},function(res){
				data=res.data;
				pageSize=res.pageSize;
				pageCount=res.pageCount;
				pageIndex=res.currpage;
				rowCount=res.rowCount;
				loadTable();
			},'json');	
		}
		
		function loadTable(){
				$('#ttitle').html('');
				for(var key in rows){
					$('#ttitle').append('<td>'+rows[key]+'</td>');
				}
				$('#ttitle').append('<td>操作</td>');
				$('#tbody').html('');
				for(var i=0;i<data.length;i++){
					var ttr ='<tr onmouseover="trOver(this)" onmouseout="trOut(this)">';
					for(var key in rows){
						ttr+='<td onclick="trClick(\''+data[i].custCid+'\');">'+data[i][key]+'</td>';
					}
					ttr+='<td><a href="fupdateCustomer.action?infoId='+data[i].custCid+'">修改</a>&nbsp;<span onclick="del(\''+data[i].custCid+'\');" >删除</span></td>';
					$('#tbody').append(ttr+'</tr>');
				}
				loadPageNumber();
		}
		
		function loadPageNumber(){
			var start =1;
			var end = pageCount;
			$('#pageIndex').html('');
			if(pageIndex>1){
				$('#pageIndex').append('<div class="page_" id="upPage">上一页</div>');
			}
			if(pageCount>9 && pageIndex>9){
				var start = pageIndex-4;
			}
			if(pageCount>(pageIndex+4)){
				 end = pageIndex+4;
			}else{
				end =pageCount;	
			}
			
			for(var i=start;i<=end;i++){
				if(pageIndex==i){
					$('#pageIndex').append('<div class="page_sel" title="当前页">'+i+'</div>');
				}else{
					$('#pageIndex').append('<div id="'+i+'" class="page_" title="第'+i+'页">'+i+'</div>');
				}
			}
			
			if(pageIndex<pageCount){
				$('#pageIndex').append('<div class="page_" id="nextPage">下一页</div>');
			}
			
			$('#page_info').html('共'+rowCount+'条  每页'+pageSize+'条 当前'+pageIndex+'/'+pageCount+'页');
			reg();
		}
		
		
		//删除
		 function del(id){
			 var result = confirm("确定删除吗?");
			 if (result) {
				 $.get("deleteCustomer.action",{"infoId":id},function(data){
						if (data=="true") {
							location.reload(true);
						}else{
							alert("删除失败！");
						}
				});
			 }
	    }
		   //tr点击事件 无刷新弹窗显示用户详细信息
		    function trClick(id){
		    	$.getJSON("showInfoCustomer.action",{"infoId":id},function(data){
		    	    if (data!=null&&data!="null") {
						$("#sinfo").html(
						 '<tr><td>省市</td><td>'+data.province+'</td></tr><tr><td>法人代表</td><td>'+data.cinfoLegal+'</td></tr><tr><td>短信</td><td>'+data.cinfoMessagephone+'</td></tr><tr><td>传真</td><td>'+data.cinfoFax+'</td></tr><tr><td>邮编</td><td>'+data.cinfoZipcode+'</td></tr><tr><td>地址</td><td>'+data.cinfoAddress+'</td></tr><tr><td>邮箱</td><td>'+data.cinfoEmail+'</td></tr><tr><td>详细信息</td><td>'+data.cinfoIntroduction+'</td></tr>'	
						);
					}else{
						$("#sinfo").html("该客户没有详细信息！");
					}
		    	});
		    	
		    	$("#showzhezhao").show();
		    	$("#showinfo").show();
		    }
		    //点击关闭
		    function cc(){
		    	$("#showzhezhao").hide();
		    	$("#showinfo").hide();
		    }
		    function trOver(obj){
		    	obj.bgColor="lime";
		    }
		    function trOut(obj){
		    	obj.bgColor="white";
		    }
		
</script>
</head>

<body>
<div id="showzhezhao"  style="MARGIN: 0px auto; display:none;  z-index:9; background-color:black;  width: 100%; height: 100%; position: absolute; " ></div>
<div id="showinfo"  style="z-index:10; display:none;  background-color:#8CC6FF;  width: 400px; height: 400px; position: absolute; margin-left: 37%; margin-top: 10%;" >
       <div class="head"><div class="head-right"></div></div>
            <div class="main">
                <h2>客户详细信息</h2>
                <input id="cc" type="button" onclick="cc();"  value="关闭">
                <table id="sinfo">
                </table>
            </div>
            <div class="foot"><div class="foot-right"></div></div>
</div>
	<div id="search">
        <s:form action="searchCustomer" theme="simple">
            姓名:<input name="name" size="12" maxlength="4" >
            电话:<input name="tellphone" size="12" maxlength="11" >
        
            客户行业:<s:select name="industry" id="industry" list="industrys" listKey="typeId" listValue="typeValue"  theme="simple"></s:select>
            客户等级:<s:select name="live" id="live" list="lives" listKey="typeId" listValue="typeValue"  theme="simple" ></s:select>
            客户性质:<s:select name="nature" id="nature" list="natures" listKey="typeId" listValue="typeValue"  theme="simple" ></s:select>
            客户类型:<s:select name="ctype" id="ctype" list="ctypes" listKey="typeId" listValue="typeValue"  theme="simple" ></s:select>
            客户来源:<s:select name="source" id="source" list="sources" listKey="typeId" listValue="typeValue"  theme="simple" ></s:select>
         <input value="  重 置  " type="reset">&nbsp;<input value="  查 询  " type="button" onclick="loadPage();">
      </s:form>
	</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<thead id="head">
  <tr id="ttitle">
  </tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<div id="page_index">
  <div id="page_info">共[10]条记录 每页5条 共2页</div>
  <div id="pageIndex"> 
    <div class="page_">1</div>
  </div>
  <div id="setPage">每页
    <label for="pageSize_num"></label>
    <input name="pageSize_num" type="text" id="pageSize_num" value="10" size="4" maxlength="4" />
  条</div>
</div>
</body>
</html>
